<template>
    <el-select
        v-model="model"
        :remote-method="fixed?null:queryList"
        :loading="loading"
        :multiple="multiple"
        :collapse-tags="multiple && collapse"
        :disabled="disabled || fixedDisabled"
        :multiple-limit="multipleLimit"
        :size="size"
        :value-key="valueKey"
        :filterable="!fixed && filterable"
        remote
        clearable
        reserve-keyword
        style="width: 100%;"
        @change="handleChange"
        :class="disabled?'deactive':'active'"
        :placeholder="placeholder?placeholder:$t('message.plzSelect')"
        v-clearreadonly
    >
        <el-option
            v-for="item in teacherList"
            :key="item.name + '-' + item.code"
            :label="item.name+ '('+item.code+')'+ '-' + item.facultyI18n"
            :value="getValue(item)"
        >
            <span style="float: left">{{ item.name + '('+item.code+')'+'-'+ item.facultyI18n }}</span>
            <!--<span :style="{'float': 'right','margin-right':multiple?'20px':'0'}">{{ item.facultyI18n }}</span>-->
        </el-option>
    </el-select>
</template>
<script>
//import { findTeacherSelectList } from 'common/src/api/studentinfo'
import { findTeacherList } from "common/src/api/resource/teachers";
import UTILS from "common/src/utils/utils";
/**
 * 老师下拉框组件
 */
export default {
    name: "TeacherSelectLong",
    model: {
        prop: "model",
        event: "change"
    },
    props: {
        model: {
            default: null,
            validator: function(value) {
                // v-model不能为null
                const valide = value !== null && value !== undefined;
                if (!valide) {
                    console.warn("v-model不能为null");
                }
                return valide;
            }
        }, // 父组件通过v-model传过来, 如果传来的值为null为出现 Cannot read property 'some' of null
        valueKey: {
            type: String,
            default: "code"
        },
        value: null,
        multiple: {
            type: Boolean,
            default: false
        },
        disabled: {
            type: Boolean,
            default: false
        },
        filterable: {
            type: Boolean,
            default: true
        },
        size: {
            type: String,
            default: ""
        },
        multipleLimit: {
            type: Number,
            default: 0
        },
        conditionNotNull: {
            // 如果为false则会在查询条件有值时才查询数据
            type: Boolean,
            default: true
        },
        condition: {
            type: Object,
            default: null
        },
        conditionChangeClear: {
            // 如果为false则会在查询条件改变时刷新options而不是添加
            type: Boolean,
            default: false
        },
        fixed: {
            // 筛选条件为固定值，即通过固定条件搜索一次 老师的值从这些options中选择 不再获取
            type: Boolean,
            default: false
        },
        placeholder: {
            type: String,
            default: ""
        },
        collapse: {
            default: true,
            type: Boolean
        },
        showAllTeacher: {
            default: false,
            type: Boolean
        },
        queryOnlyByTeacherId: {
            default: false,
            type: Boolean
        }
    },
    data() {
        return {
            teacherList: [],
            loading: false
        };
    },
    computed: {
        fixedDisabled() {
            return this.fixed && this.teacherList.length == 0;
        }
    },
    mounted() {
        this.init();
    },
    created() {},
    watch: {
        condition: {
            handler: function(newVal, oldVal) {
                this.init();
            }
        }
    },
    methods: {
        handleChange(val) {
            this.$emit("change", val);
        },
        getValue(item) {
            if (UTILS.isEmpty(this.value)) {
                return item;
            }
            return item[this.value];
        },
        init() {
            if (this.conditionNotNull && UTILS.isEmpty(this.condition)) {
                return;
            }
            let queryData = UTILS.clone(
                this.condition == null ? {} : this.condition
            );
            if (
                _.isNull(queryData.pageSize_) ||
                _.isUndefined(queryData.pageSize_)
            ) {
                queryData.pageSize_ = 50;
            }
            if (
                _.isNull(queryData.pageNum_) ||
                _.isUndefined(queryData.pageNum_)
            ) {
                queryData.pageNum_ = 1;
            }
            if (this.showAllTeacher) {
                queryData.showAllTeacher = "1";
            }
            if (this.fixed) {
                if (UTILS.isNotEmpty(queryData.codeList)) {
                    findTeacherList(queryData).then(res => {
                        const teacherList = res.data.list;
                        if (this.conditionChangeClear) {
                            this.teacherList = teacherList;
                        } else {
                            if (teacherList.length > 0) {
                                let set = new Set(teacherList.map(t => t.code));
                                this.teacherList = teacherList.concat(
                                    this.teacherList.filter(
                                        item => !set.has(item.code)
                                    )
                                );
                            }
                        }
                        this.loading = false;
                    });
                } else {
                    this.loading = false;
                    console.warn("使用了fixed属性却没有设置codelist");
                }
            } else {
                let tempData = queryData;
                if (UTILS.isNotEmpty(queryData.codeList)) {
                    tempData = { codeList: queryData.codeList };
                    
                    if (this.showAllTeacher) {
                        tempData.showAllTeacher = "1";
                    }
                }
                findTeacherList(tempData).then(res => {
                    this.teacherList = res.data.list;
                    // 补充查询待选列表
                    if (UTILS.isNotEmpty(queryData.codeList)) {
                        // queryData.codeList = null;
                        findTeacherList(queryData).then(res => {
                            const teacherList = res.data.list;
                            if (this.conditionChangeClear) {
                                this.teacherList = teacherList;
                            } else {
                                if (teacherList.length > 0) {
                                    let set = new Set(
                                        teacherList.map(t => t.code)
                                    );
                                    this.teacherList = teacherList.concat(
                                        this.teacherList.filter(
                                            item => !set.has(item.code)
                                        )
                                    );
                                }
                            }
                            this.loading = false;
                        });
                    } else {
                        this.loading = false;
                    }
                });
            }
        },
        queryList(query) {
            if (query !== "") {
                this.loading = true;
                let condition = UTILS.clone(
                    this.condition == null
                        ? { pageSize_: 50, pageNum_: 1 }
                        : this.condition
                );
                if (this.showAllTeacher) {
                    condition.showAllTeacher = "1";
                }
                condition.condition = query;
                if (this.queryOnlyByTeacherId) {
                    condition.keyword = query;
                }
                condition.codeList = null;
                findTeacherList(condition).then(res => {
                    this.teacherList = res.data.list;
                    this.loading = false;
                });
            }
        }
    }
};
</script>

<style scoped>
</style>
